package ntu.metaweb.update;

import java.io.IOException;

import ntu.metaweb.XMLMarshaller;
import ntu.metaweb.storage.Rating;
import ntu.p2p.dht.messaging.DHTObjectDifference;
import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;

public class UpdatePageRating implements DHTObjectDifference {

	protected static final short TYPE = 1;
	
	protected Rating rating;
	
	protected UpdatePageRating(InputBuffer buf) {
		try {
			int length = buf.readInt();
			byte[] temp = new byte[length];
			buf.read(temp, 0, length);
			this.rating = (Rating) XMLMarshaller.getInstance().unmarshall(new String(temp));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public UpdatePageRating(Rating rating) {
		this.rating = rating;
	}

	public static DHTObjectDifference build(InputBuffer buf) {
		return new UpdatePageRating(buf);
	}
	
	@Override
	public void serialize(OutputBuffer buf) throws IOException {
		buf.writeShort(TYPE);
		byte[] temp = XMLMarshaller.getInstance().marshall(rating).getBytes();
		buf.writeInt(temp.length);
		buf.write(temp, 0, temp.length);
	}

}
